package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 服务相关配置
 *
 * @author
 */
@Slf4j
@Component
public class ServerConfig {

    @Value("${spring.profiles.active}")
    private String pathSign;
    @Value("${ruoyi.requestIp}")
    private String requestIp;
    @Value("${ruoyi.domain}")
    private String domain;
    @Value("${server.port}")
    private String port;

    /**
     * 获取完整的请求路径，包括：域名，端口，上下文访问路径
     *
     * @return 服务地址
     */
    public String getUrl() {
        HttpServletRequest request = ServletUtils.getRequest();
        return getDomain(request);
    }

    public String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String contextPath = request.getServletContext().getContextPath();
        String urlString = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
//        // 正式环境IP登录时，IP登录的路径改为域名
//        if ("prod".equals(pathSign)) {
//            if (urlString.contains(requestIp)) {
//                return urlString.replaceAll("http", "https").replaceAll(":" + port, "").replaceAll(requestIp, domain);
//            } else if (urlString.contains(domain)) {
//                // 处理域名访问带了端口
//                return urlString.replaceAll("http", "https").replaceAll(":" + port, "");
//            }
//        }
        return urlString;
    }
}
